#!/usr/bin/env python
# -*- coding: utf-8 -*-
# Author:zhangcong
# Email:zc_92@sina.com



from django.forms import Form
from django.forms import widgets
from django.forms import fields

from web import models


# 添加客户 Form
class clientinfoForms(Form):
    client_name = fields.CharField(
        error_messages={
            "required": "客户名称不能为空！",
        },
    )
    contact_name = fields.CharField(
        required=False,
    )
    phone = fields.CharField(
        required=False,
    )
    qq = fields.CharField(
        required=False,
    )

    status = fields.CharField(
        error_messages={
            "required": "请选择跟进状态！",
        },
    )
    remark = fields.CharField(
        required=False,
    )

    def clean_client_name(self):
        client_name = self.data['client_name']  # 客户名称

        obj = models.ClientInfo.objects.filter(client_name=client_name)
        print(obj)

        if obj:
            self.errors['client_name'] = "客户名称已存在!"
        else:
            return self.data['client_name']


# 修改客户 Form
class clientinfoForms_update(Form):
    client_name = fields.CharField(
        error_messages={
            "required": "客户名称不能为空！",
        },
    )
    contact_name = fields.CharField(
        required=False,
    )
    phone = fields.CharField(
        required=False,
    )
    qq = fields.CharField(
        required=False,
    )

    status = fields.CharField(
        error_messages={
            "required": "请选择跟进状态！",
        },
    )

    id = fields.CharField(
        error_messages={
            "required": "客户id 为非法操作！",
        },
    )
    remark = fields.CharField(
        required=False,
    )

    def clean_name(self):
        client_name = self.data['client_name']  # 客户名称
        client_id = self.data['id']  # 客户id

        obj = models.ClientInfo.objects.exclude(id=client_id).filter(client_name=client_name)

        if obj:
            self.errors['client_name'] = "客户名称已存在!"
        else:
            return self.data['client_name']

